using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Rensoft.CRM.Client.AccountsAPI;

namespace Rensoft.CRM.Client.Editors
{
    public partial class PaymentEditor : Rensoft.CRM.Client.Editors.BaseEditor
    {
        private Account account;
        private PaymentType paymentType;

        protected PaymentEditor()
        {
            InitializeComponent();
        }

        public PaymentEditor(Program program) : base(program)
        {
            InitializeComponent();

            SetMode(EditorMode.Create);
            Text = "New Payment - " + AssemblyInfo.Title;
        }

        protected override void PerformClear()
        {
            base.PerformClear();

            /* Intentionally leave date the same.
             * Currency automatically resets. */

            recieveRadioButton.Checked = true;
            typeComboBox.SelectedIndex = 0;
            amountTextBox.ResetText();
            referenceTextBox.ResetText();
        }

        protected override void PerformLoad(DoWorkEventArgs e)
        {
            base.PerformLoad(e);
            InitializeAccountComboBox(accountComboBox, AccountType.Client | AccountType.Supplier);
            InitializePaymentTypeComboBox(typeComboBox);

            switch (Mode)
            {
                case EditorMode.Modify:
                    {
                        SaveToolStripButton.Enabled = false;
                        SaveNewToolStripButton.Enabled = false;
                        SaveCloseToolStripButton.Enabled = false;
                    }
                    break;
            }
        }

        protected void InitializePaymentTypeComboBox(ComboBox comboBox)
        {
            List<PaymentTypeComboItem> list = new List<PaymentTypeComboItem>();
            list.Add(new PaymentTypeComboItem(PaymentType.Cheque, "Cheque"));
            list.Add(new PaymentTypeComboItem(PaymentType.BalanceTransfer, "Balance Transfer"));
            list.Add(new PaymentTypeComboItem(PaymentType.Bacs, "BACS"));
            list.Add(new PaymentTypeComboItem(PaymentType.Cash, "Cash"));
            comboBox.DataSource = list;
        }

        protected override void PerformSave(DoWorkEventArgs e)
        {
            base.PerformSave(e);

            PaymentDirection direction = PaymentDirection.Recieve;
            if (sendRadioButton.Checked)
            {
                direction = PaymentDirection.Send;
            }

            decimal amount = decimal.Parse(amountTextBox.Text);

            AccountsService service = AccountsService.CreateInstance();

            switch (Mode)
            {
                case EditorMode.Create:
                    {
                        service.PaymentCreate(
                            paymentType, direction, amount,
                            dateMonthCalendar.SelectionStart,
                            referenceTextBox.Text, account.AccountId);
                    }
                    break;
            }
        }

        private void accountComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            account = ((AccountComboItem)accountComboBox.SelectedItem).Value;
            currencyTextBox.Text = account.Currency;
        }

        private void typeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            paymentType = (PaymentType)typeComboBox.SelectedValue;
        }
    }

    internal class PaymentTypeComboItem
    {
        private PaymentType paymentType;
        private string description;

        public PaymentType Value
        {
            get { return paymentType; }
        }

        public string Display
        {
            get { return description; }
        }

        public PaymentTypeComboItem(PaymentType paymentType, string description)
        {
            this.paymentType = paymentType;
            this.description = description;
        }
    }
}

